package com.gift.recommendation.modules.admin.biz;

import com.gift.recommendation.Base.PageData;
import com.gift.recommendation.modules.admin.dao.entity.FestivalRelationEntity;
import com.gift.recommendation.modules.admin.service.FestivalRelationService;
import com.gift.recommendation.util.PageTransUtil;
import com.gift.recommendation.modules.admin.dao.entity.FestivalEntity;
import com.gift.recommendation.modules.admin.dto.FestivalListReq;
import com.gift.recommendation.modules.admin.dto.FestivalRsp;
import com.gift.recommendation.modules.admin.dto.FestivalReq;
import com.gift.recommendation.modules.admin.service.FestivalService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author zzh <2658137258@qq.com>
 * @date 2023/1/16
 */
@Service
public class FestivalImpl {

    @Autowired
    private FestivalService festivalService;

    @Autowired
    private FestivalRelationService festivalRelationService;

    /**
     * 添加节假日
     * @param req
     */
    public void addFestival(FestivalReq req){
        FestivalEntity festival = new FestivalEntity();
        festival.setUuid(UUID.randomUUID().toString());
        festival.setName(req.getName());
        festival.setStartTime(req.getStartDate());
        festival.setEndTime(req.getEndDate());
        festivalService.insertSelective(festival);
    }

    /**
     * 修改节假日信息
     * @param req
     */
    public void changeFestival(FestivalReq req) {
        FestivalEntity festival = new FestivalEntity();
        festival.setId(req.getId());
        festival.setName(req.getName());
        festival.setStartTime(req.getStartDate());
        festival.setEndTime(req.getEndDate());
        festivalService.updateByPrimaryKeySelective(festival);
    }

    /**
     * 删除节假日信息
     * @param id
     */
    public void deleteFestival(Long id) {
        FestivalEntity festival = festivalService.selectByPrimaryKey(id);
        Example example = new Example(FestivalRelationEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("festivalUuid",festival.getUuid());
        festivalRelationService.deleteByExample(example);
        festivalService.deleteByPrimaryKey(id);
    }

    /**
     * 查找节日列表
     * @param req
     */
    public PageData<FestivalRsp> festivalList(FestivalListReq req) {
        //mybatis分页
        com.github.pagehelper.Page<FestivalRsp> page = PageHelper.startPage(
                req.getCurrentPage(), req.getPageSize()
        );
        festivalService.searchList(req);
        return PageTransUtil.createPageList(page, FestivalRsp.class);
    }

    public FestivalRsp selectByPrimaryKey(Long id) {
        FestivalRsp rsp = new FestivalRsp();
        BeanUtils.copyProperties(festivalService.selectByPrimaryKey(id),rsp);
        return rsp;
    }

    /**
     * 获取当前最近节日信息
     * @return
     */
    public FestivalRsp getPass() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        Date var = calendar.getTime();
        Example exampleFestival = new Example(FestivalEntity.class);
        Example.Criteria criteriaFestival = exampleFestival.createCriteria();
        criteriaFestival.andGreaterThanOrEqualTo("endTime",var);
        criteriaFestival.andLessThanOrEqualTo("startTime",var);
        List<FestivalEntity> list = festivalService.selectByExample(exampleFestival);
        FestivalRsp festivalRsp = new FestivalRsp();
        if (CollectionUtils.isNotEmpty(list)) {
            BeanUtils.copyProperties(list.get(0),festivalRsp);
        } else {
            exampleFestival = new Example(FestivalEntity.class);
            criteriaFestival = exampleFestival.createCriteria();
            criteriaFestival.andGreaterThanOrEqualTo("startTime",var);
            exampleFestival.orderBy("startTime").asc();
            List<FestivalEntity> list1 = festivalService.selectByExample(exampleFestival);
            if (CollectionUtils.isNotEmpty(list1)) {
                BeanUtils.copyProperties(list1.get(0),festivalRsp);
            }
        }
        return festivalRsp;
    }
}
